import { queryRoleList, changeRoleStatus, deleteRoleById } from "@/api/system/role";
import { SystemStatus } from "@/types/emum";
import { Role } from "@/types/system/role";
import { message } from "antd";
import { useState, useCallback, useEffect } from "react";

export const useRoleList = () => {
    const [messageApi] = message.useMessage();
    const [loading, setLoading] = useState(false);
    const [searchForm, setSearchForm] = useState<Role.Query>({
        remark: "",
        roleName: "",

    });

    const [tableData, setTableData] = useState<Role.Row[]>([]);
    /**
     * 获取标签列表
     */
    const getRoleList = useCallback(async () => {
        setLoading(true);
        const params = {
            ...searchForm,
        };
        const res = await queryRoleList(params);
        setLoading(false);
        if (res.code === 200) {
            setTableData(res.data || []);
        }
    }, [searchForm]);
    /**
   * 修改角色状态
   * @param role
   */
    const roleStatusChange = async (role: Role.Row) => {
        const { id: roleId, status } = role;
        const params: Role.StatusVo = {
            roleId,
            status: status === SystemStatus.normal ? SystemStatus.disabled : SystemStatus.normal,
        };
        setLoading(true)
        await changeRoleStatus(params);
        getRoleList()
    };
    /**
     * 删除角色
     * @param row 
     */
    const removeRole = async (row: Role.Row) => {
        const res = await deleteRoleById(row.id)
        if (res.code === 200) {
            messageApi.success('删除成功!')
            getRoleList()
        }
    }
    useEffect(() => {
        getRoleList();
    }, [getRoleList, searchForm]);
    return {
        removeRole,
        tableData,
        loading,
        roleStatusChange,
        setSearchForm,
        getRoleList

    }
}